﻿/*
CopyLock
=====================

Version: 1.0

Requisites: jQuery, rsuite.copylock.css

Features: restrict selection on an element only (to prevent unwanted selection when dragging)

Usage:
- Add `copy-restricted` class to target element
*/
$(document).ready(function () {
    var restrictSelection = function (element) {
        var cur = element[0];
        element.parents().each(function () {
            $(this).children().each(function () {
                if (this !== cur) {
                    $(this).addClass('noselect').attr('unselectable', 'on');
                }
            });
            cur = this;
        });
    };
    var allowSelection = function (element) {
        $(window).unbind('mouseup');
        $(window).unbind('dragend');
        $('.noselect').removeClass('noselect').removeAttr('unselectable');
    };

    $('.copy-restricted').mousedown(function () {
        var self = $(this);
        if (!self.data('mousedown')) {
            self.data('mousedown', true);
            restrictSelection(self);

            $(window).mouseup(function () {
                self.data('mousedown', false);
                allowSelection();
            });
            $(window).bind('dragend', function () {
                self.data('mousedown', false);
                allowSelection();
            });
        }
    });
});